//
//  CXStatus.swift
//  Project03-Weibo-swift
//
//  Created by w on 16/1/22.
//  Copyright © 2016年 w. All rights reserved.
//

import UIKit

class CXStatus: NSObject {
    
    /// 微博的内容
    var text: String?
    /// 微博的创建时间
    var created_at: String? {
        
        didSet {
            //一旦赋值完毕 就给自定义属性 create_date赋值
            created_date = NSDate.sinaCreateDate(sinaStr: created_at)
        }
    }
    
    var created_date: NSDate?
    /// 来源
    var source: String?
    /// 当前微博的发送者
    var user: CXUser?
    /// 转发微博的模型
    var retweeted_status: CXStatus?
    /// 转发数
    var reposts_count: Int = 0
    /// 评论数
    var comments_count: Int = 0
    /// 表态数
    var attitudes_count: Int = 0
    /// 图片链接组
    var pic_urls:[CXStatusPhotoInfo]?
    /// 索引, status_id
    var id: Int64 = 0
    
    init(dict: [String:AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value:AnyObject?, forUndefinedKey key: String) {
        //do nothing
    }
    ///  重写setter，给里面的 user 继续转模型
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" && value != nil {
            user = CXUser(dict: value as! [String:AnyObject])
            return
        } else if key == "retweeted_status" && value != nil {
            //如果有转发 就另外创建一个模型
            retweeted_status = CXStatus(dict: value as! [String:AnyObject])
            return
        }else if key == "pic_urls" {
            var tempArry = [CXStatusPhotoInfo]()
            for dict in value as! [[String:AnyObject]] {
                tempArry.append(CXStatusPhotoInfo(dict: dict))
            }
            pic_urls = tempArry
            return
        }
        super.setValue(value, forKey: key)
    }
}
